package com.ljb.readaloud.cos;

import android.content.Context;
import android.util.Log;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.model.object.PutObjectRequest;

/**
 * Created by ljb on 2018/3/29.
 */

public class COSUtils {
    private static final String TAG = "COSUtils";
    private static COSUtils instance;

    private static final String appid = "1253658271";
    private static final String region = "ap-beijing";
    private static final String secretId = "AKIDJRL0Qh1W5QXP6Z9lUyEG2ZruiHdkPGiv";
    private static final String secretKey = "5vYw5zd2D9qOhmcFdlEXbQ0LRIuHRM2U";
    private static final String bucket = "langduzhe-1253658271";
    private static final long keyDuration = 600;
    private static final long signDuration = 600;


    public static COSUtils getInstance() {
        if (instance == null)
            instance = new COSUtils();
        return instance;
    }

    private OnCOSUploadListener mOnCOSUploadListener;

    public void setOnCOSUploadListener(OnCOSUploadListener cosUploadListener) {
        mOnCOSUploadListener = cosUploadListener;
    }

    private CosXmlService initCos(Context context) {
        //创建 CosXmlServiceConfig 对象，根据需要修改默认的配置参数
        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setAppidAndRegion(appid, region)
                .setDebuggable(true)
                .builder();
        //创建获取签名类(请参考下面的生成签名示例，或者参考 sdk中提供的ShortTimeCredentialProvider类）
        LocalCredentialProvider localCredentialProvider = new LocalCredentialProvider(secretId, secretKey, keyDuration);
        //创建 CosXmlService 对象，实现对象存储服务各项操作.
        return new CosXmlService(context, serviceConfig, localCredentialProvider);
    }

    /**
     * @param context
     * @param cosPath
     * @param srcPath
     */
    public void upLoad(Context context, String cosPath, String srcPath) {
        CosXmlService cosXmlService = initCos(context);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);
        putObjectRequest.setSign(signDuration, null, null); //若不调用，则默认使用sdk中sign duration（60s）
        //进度监听
        putObjectRequest.setProgressListener(new CosXmlProgressListener() {
            @Override
            public void onProgress(long progress, long max) {
                float result = (float) (progress * 100.0 / max);
                Log.e(TAG, "progress =" + (long) result + "%");

                if (mOnCOSUploadListener != null) {
                    mOnCOSUploadListener.onProgress(progress, max);
                }
            }
        });
        //异步上传
        cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest request, CosXmlResult result) {
                Log.e(TAG, "success =" + result.accessUrl);

                if (mOnCOSUploadListener != null) {
                    mOnCOSUploadListener.onSuccess(request, result);
                }
            }

            @Override
            public void onFail(CosXmlRequest cosXmlRequest, CosXmlClientException clientException, CosXmlServiceException serviceException) {
                String errorMsg = clientException != null ? clientException.toString() : serviceException.toString();
                Log.e(TAG, errorMsg);

                if (mOnCOSUploadListener != null) {
                    mOnCOSUploadListener.onError(cosXmlRequest, clientException, serviceException);
                }
            }
        });
    }
}
